package elements 
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * 
	 * @author kasoi
	 */
	public class DrawboardItem extends Sprite 
	{
		static public const PENCIL: String = 'pencil';
		
		static public const LINE: String = 'line';
		
		static public const CIRCLE: String = 'circle';
		
		static public const CIRCLE_FILLED: String = 'circleFilled';
		
		static public const RECTANGLE: String = 'rectangle';
		
		static public const RECTANGLE_FILLED: String = 'rectangleFilled';
		
		static public const ARROW: String = 'arrow';
		
		static public const TEXT: String = 'text';
		
		////////////////////////////////////////////////////////////////////
		// Public properties
		////////////////////////////////////////////////////////////////////
		
		public function get type() : String 
		{ 
			return this._type; 
		}
		
		public function get thickness() : Number 
		{ 
			return this._thickness; 
		}
		
		public function get color() : Number 
		{ 
			return this._color; 
		}
		
		public function get transparency() : Number 
		{ 
			return this._transparency; 
		}
		
		public function get bounds() : Rectangle 
		{ 
			return this._bounds; 
		}
		
		public function get shiftPressed() : Boolean 
		{ 
			return this._shiftPressed; 
		}
		
		////////////////////////////////////////////////////////////////////
		// Private properties
		////////////////////////////////////////////////////////////////////
		
		private var item: Shape;
		
		private var _type: String;
		
		private var _thickness: Number;
		
		private var _color: Number;
		
		private var _transparency: Number;
		
		private var _bounds: Rectangle;
		
		private var _shiftPressed: Boolean;
		
		////////////////////////////////////////////////////////////////////
		// Public methods
		////////////////////////////////////////////////////////////////////
		
		/*
		 * Самая рисовательная фигня. Как только этот элемент создан, он 
		 * начинает сразу же рисоваться, поэтому его нужно ставить в координаты
		 * мыши, чтоб он рисовался относительно нее
		 * 
		 * @param type - тип рисования [DrawboardItem.PENCIL и т.д.]
		 * @param bounds - границы, в пределах которых может рисоваться что-либо
		 * если не заданы, то будет 3000х3000 квадрат, отцентрированный
		 * @param thickness - 
		 * @param color -
		 * @param transparency - 
		 */
		public function DrawboardItem(type: String, bounds: Rectangle = null,
			thickness: Number = 1, color: Number = 0, transparency: Number = 1) 
		{
			if (bounds == null) bounds = new Rectangle( -1500, -1500, 3000, 3000);
			this._bounds = bounds;
			this._type = type;
			this._thickness = thickness;
			this._color = color;
			this._transparency = transparency;
			this.init();
		}
		
		public function destroy() : void 
		{
			this.clearListeners();
		}
		
		public function drawToBitmap() : void 
		{
			
		}
		
		////////////////////////////////////////////////////////////////////
		// Private methods
		////////////////////////////////////////////////////////////////////
		
		private function init() : void
		{
			
			if (this.type == DrawboardItem.TEXT)
			{
				this.drawTextField();
			}
			else
			{
				this.drawItem();
			}
		}
		
		private function drawTextField() : void 
		{
			
		}
		
		private function drawItem() : void 
		{
			this.item = new Shape();
			this.addChild(this.item);
			
			StageManager.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.stage_onMouseMove);
			StageManager.stage.addEventListener(MouseEvent.MOUSE_UP, this.stage_onMouseUp);
			StageManager.stage.addEventListener(KeyboardEvent.KEY_DOWN,
				this.stage_onKeyDown);
			StageManager.stage.addEventListener(KeyboardEvent.KEY_UP,
				this.stage_onKeyUp);
		}
		
		private function clearListeners() : void 
		{
			StageManager.stage.removeEventListener(MouseEvent.MOUSE_MOVE, 
				this.stage_onMouseMove);
			StageManager.stage.removeEventListener(MouseEvent.MOUSE_UP, 
				this.stage_onMouseUp);
			StageManager.stage.removeEventListener(KeyboardEvent.KEY_DOWN,
				this.stage_onKeyDown);
			StageManager.stage.removeEventListener(KeyboardEvent.KEY_UP,
				this.stage_onKeyUp);
		}
		
		////////////////////////////////////////////////////////////////////
		// Listeners
		////////////////////////////////////////////////////////////////////
		
		private function stage_onMouseMove(e: MouseEvent) : void 
		{
			var mx: Number = this.mouseX;
			var my: Number = this.mouseY;
			
			var bx: Number = this._bounds.x;
			var by: Number = this._bounds.y;
			var bw: Number = this._bounds.width + bx;
			var bh: Number = this._bounds.height + by;
			
			if (this.shiftPressed)
			{
				var biggest: Number = mx;
				if (mx * mx < my * my) biggest = my;
				
				mx = my = biggest;
			}
			
			if (mx < bx) mx = bx;
			if (mx > bw) mx = bw;
			if (my < by) my = by;
			if (my > bh) my = bh;
			
			if (this._type == DrawboardItem.PENCIL)
			{
				this.item.graphics.lineStyle(this._thickness, this._color, this._transparency);
				this.item.graphics.lineTo(mx, my);
				return;
			}
			
			var g: Graphics = this.item.graphics;
			g.clear();
			
			if (this._type == DrawboardItem.ARROW)
			{
				var d: Number = Math.sqrt((0 - mx) * (0 - mx) + (0 - my) * (0 - my));
				g.lineStyle(this._thickness, this._color, this._transparency);
				g.lineTo(d, 0);
				g.lineTo(d - 5 * this._thickness, -2.5 * this._thickness);
				g.moveTo(d, 0);
				g.lineTo(d - 5 * this._thickness, 2.5 * this._thickness);
				
				this.item.rotation = Math.atan2(my, mx) * (180 / Math.PI);
			}
			if (this._type == DrawboardItem.RECTANGLE)
			{
				g.lineStyle(this._thickness, this._color, this._transparency);
				g.drawRect(0, 0, mx, my);
			}
			if (this._type == DrawboardItem.RECTANGLE_FILLED)
			{
				g.beginFill(this._color, this._transparency);
				g.drawRect(0, 0, mx, my);
			}
			if (this._type == DrawboardItem.CIRCLE)
			{
				g.lineStyle(this._thickness, this._color, this._transparency);
				g.drawEllipse(0, 0, mx, my);
			}
			if (this._type == DrawboardItem.CIRCLE_FILLED)
			{
				g.beginFill(this._color, this._transparency);
				g.drawEllipse(0, 0, mx, my);
			}
			if (this._type == DrawboardItem.LINE)
			{
				g.lineStyle(this._thickness, this._color, this._transparency);
				g.lineTo(mx, my);
			}
			
			g.endFill();
		}
		
		private function stage_onMouseUp(e: MouseEvent) : void 
		{
			this.clearListeners();
		}
		
		private function stage_onKeyDown(e: KeyboardEvent) : void 
		{
			this._shiftPressed = e.shiftKey;
		}
		
		private function stage_onKeyUp(e: KeyboardEvent) : void 
		{
			this._shiftPressed = e.shiftKey;
		}
		
	}
	
}